<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class DoctorInfoController extends Controller
{
    protected function echoJson($arr)
    {//内部函数
        echo json_encode($arr);
    }
        //===========================显示医生的信息=====================================
    public function lists()
    {
        if(I('request.id')==""){
            $this->echoJson(array('result'=>1,'info'=>'参数错误'));
        }else{
            $id=I('request.id');
            $Doctor=D('Doctor');
            $Hospital=D('Hospital');
            $auth_status=$Doctor->where('id='.$id)->getField('auth_status');
            if($auth_status!=2){
                $this->echoJson(array('result'=>1,'info'=>'错误的操作'));
            }else{
            $map['auth_status']=$auth_status;
            $map['id']=$id;
            $result=array();
            $doctorInfo=$Doctor->where($map)
                ->field('name,sex,img,auth_img,speciality_en,speciality_cn,abstract,descrip,hospital_id,document_number,title_cn,title_en')
                ->find();
            if($doctorInfo){
                $hospital_id=$doctorInfo['hospital_id'];
                $hospitalName=$Hospital->where('id='.$hospital_id)->getField('name');
                if($hospitalName){
                    $doctorInfo['img']=(!empty($doctorInfo['img']))? "http://img.cndoc.ivf-bb.com/".$doctorInfo['img']:$doctorInfo['img'];
                    $doctorInfo['auth_img']=(!empty($doctorInfo['auth_img']))? "http://img.cndoc.ivf-bb.com/".$doctorInfo['auth_img']:$doctorInfo['auth_img'];
                    $doctorInfo['hospitalName']=$hospitalName;
                    $doctorInfo['sex']=$doctorInfo['sex']==0 ? "女" : "男";
                    $result[]=$doctorInfo;
                    $this->echoJson(array('result'=>0,'info'=>$result));
                }
            }
          }
        }
    }

    public function image(){
        if(I('request.id')==""){
            $this->echoJson(array('result'=>1,'info'=>'参数错误'));
        }else{
            $Doctor=D("Doctor");
            $id=I("request.id");
            $status=$Doctor->where("id=".$id)->getField('auth_status');
            if($status!=2){
                $this->echoJson(array('result'=>1,'info'=>'错误操作'));
            }else{
                if($_FILES['img']!=null){

                    //七牛的上传
                    $setting=C('UPLOAD_SITEIMG_QINIU');
                    $setting['driverConfig']['domain']='img.cndoc.ivf-bb.com';
                    $setting['driverConfig']['bucket'] = 'imagecndoc';
                    // $setting['saveName'] = array ('uniqid', 'imagePYQ_');
                    $setting['subName'] = date('Y-m-d',time());
                    $Upload = new \Think\Upload($setting);
                    $info = $Upload->upload($_FILES);
                    $t=  date('Y-m-d',time());
                    $name='';
                    foreach ($info as $v){
                        $name.=$t.'/'.$v['savename'];
                    }
                    //数据库保存数据
                    $data['img'] = $name;
                    $Doctor->where("id=" . $id)->save($data);
                    if ($Doctor) {
                        $imgDisplay = $Doctor->where("id=" . $id)->getField("img");
                        if ($imgDisplay) {
                            $img = "http://img.cndoc.ivf-bb.com/" . $imgDisplay;
                            $this->echoJson(array('result' => 0, 'info' => $img));
                        } else {
                            $this->echoJson(array('result' => 1, 'info' => '图片上传失败'));
                        }
                    }
            }
          }
      }
  }
    public function actionInput(){
        if(I('request.id')==''){
            $this->echoJson(array('result'=>1,'info'=>'参数错误'));
        }else{
            $id=I('request.id');
            if(I('request.descrip')!=''){
                $data['descrip']=I('request.descrip');

            }
            if(I('request.abstract')!=''){
                $data['abstract']=I('request.abstract');
            }

            $data['update_time']=time();
            $Doctor=D('Doctor');
            $result=$Doctor->where('id='.$id)->save($data);

            if(!$result){
                $this->echoJson(array('result'=>1,'info'=>'修改失败'));
            }else{
                $this->echoJson(array('result'=>0,'info'=>"修改成功"));
            }
        }

    }

}
